home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_usrdoc / PROCMAIL.{23 / EXAMPLES / MAILSTAT < prev   
Text File  |  1999-09-17  |  6KB  |  215 lines

  1. #! /bin/sh
  2. : &&O='cd .' || exec /bin/sh "$0" $argv:q # we're in a csh, feed myself to sh
  3. $O || exec /bin/sh "$0" "$@"          # we're in a buggy zsh
  4. #################################################################
  5. #    mailstat    shows mail-arrival statistics        #
  6. #                                #
  7. #    Parses a procmail-generated $LOGFILE and displays    #
  8. #    a summary about the messages delivered to all folders    #
  9. #    (total size, average size, nr of messages).        #
  10. #    Exit code 0 if mail arrived, 1 if no mail arrived.    #
  11. #                                #
  12. #    For help try, "mailstat -h"                #
  13. #                                #
  14. #    Customise to your heart's content, this file is only    #
  15. #    provided as a guideline.                #
  16. #                                #
  17. #    Created by S.R. van den Berg, The Netherlands        #
  18. #    This file can be freely copied for any use.        #
  19. #################################################################
  20. #$Id: mailstat,v 1.24.2.1 1999/04/06 03:35:03 guenther Exp $
  21.  
  22. #    This shell script expects the following programs to be in the
  23. #    PATH (paths given here are the standard locations, your mileage
  24. #    may vary (if the programs can not be found, extend the PATH or
  25. #    put their absolute pathnames in here):
  26.  
  27. test=test        # /bin/test
  28. echo=echo        # /bin/echo
  29. expr=expr        # /bin/expr
  30. tty=tty            # /bin/tty
  31. sed=sed            # /bin/sed
  32. sort=sort        # /bin/sort
  33. awk=awk            # /usr/bin/awk
  34. cat=cat            # /bin/cat
  35. mv=mv            # /bin/mv
  36. ls=ls            # /bin/ls
  37.  
  38. PATH=/bin:/usr/bin
  39. SHELL=/bin/sh        # just in case
  40. export SHELL PATH
  41.  
  42. umask 077        # we don't allow everyone to read the tmpfiles
  43. OLDSUFFIX=.old
  44.  
  45. DEVNULL=/dev/null
  46. EX_USAGE=64
  47.  
  48. ########
  49. #    (Concatenated) flags parsing in pure, portable, structured (it
  50. #    would have been more elegant if gotos were permitted) shellscript
  51. #    language.  For added pleasure: a quick demonstration of the shell's
  52. #    quoting capabilities :-).
  53. ########
  54.  
  55. while $test $# != 0 -a a"$1" != a-- -a \
  56.  \( 0 != `$expr "X$1" : X-.` -o $# != 1 \)
  57. do
  58.   if $expr "X$1" : X-. >$DEVNULL    # structured-programming spaghetti
  59.   then
  60.      flags="$1"; shift
  61.   else
  62.      flags=-h                # force help page
  63.   fi
  64.   while flags=`$expr "X$flags" : 'X.\(.*\)'`; $test ."$flags" != .
  65.   do
  66.      case "$flags" in
  67.      k*) MSkeeplogfile=1;;
  68.      l*) MSlong=1;;
  69.      m*) MSmergerror=1;;
  70.      o*) MSoldlog=1; MSkeeplogfile=1;;
  71.      t*) MSterse=1;;
  72.      s*) MSsilent=1;;
  73.      h*|\?*) $echo 'Usage: mailstat [-klmots] [logfile]' 1>&2
  74.         $echo '    -k    keep logfile intact' 1>&2
  75.         $echo '    -l    long display format' 1>&2
  76.         $echo '    -m    merge any errors into one line' 1>&2
  77.         $echo '    -o    use the old logfile' 1>&2
  78.         $echo '    -t    terse display format' 1>&2
  79.         $echo '    -s    silent in case of no mail' 1>&2
  80.         exit $EX_USAGE;;
  81.      *) $echo 'Usage: mailstat [-klmots] [logfile]' 1>&2; exit $EX_USAGE;;
  82.      esac
  83.   done
  84. done
  85.  
  86. $test a"$1" = a-- && shift
  87.  
  88. LOGFILE="$1"
  89.  
  90. case "$LOGFILE" in
  91.   *$OLDSUFFIX) MSkeeplogfile=1; OLDLOGFILE="$LOGFILE";;
  92.   *) OLDLOGFILE="$LOGFILE$OLDSUFFIX";;
  93. esac
  94.  
  95. if test .$MSoldlog = .1
  96. then
  97.   LOGFILE="$OLDLOGFILE"
  98. fi
  99.  
  100. if $test ."$LOGFILE" != .- -a ."$LOGFILE" != .
  101. then
  102.   if $test ! -s "$LOGFILE"
  103.   then
  104.      if $test .$MSsilent = .
  105.      then
  106.     if $test -f "$LOGFILE"    # split up the following nested backquote
  107.     then            # expression, some shells (NET2) choked on it
  108.        info=`LANG= LC_TIME= $ls -l "$OLDLOGFILE"`
  109.        $echo No mail arrived since \
  110.         `$expr "X$info" : \
  111.          '.*[0-9] \(... .[^ ] .....\) [^ ]'`
  112.     else
  113.        $echo "Can't find your LOGFILE=$LOGFILE"
  114.     fi
  115.      fi
  116.      exit 1
  117.   fi
  118. else
  119.   if $test ."$LOGFILE" != .- && $tty -s
  120.   then
  121.      $echo \
  122.       "Most people don't type their own logfiles;  but, what do I care?" 1>&2
  123.      MSterse=1
  124.   fi
  125.   MSkeeplogfile=1; LOGFILE=
  126. fi
  127.  
  128. if $test .$MSkeeplogfile = .
  129. then $mv "$LOGFILE" "$OLDLOGFILE"; $cat $DEVNULL >>"$LOGFILE"
  130. else OLDLOGFILE="$LOGFILE"
  131. fi
  132.  
  133. if $test .$MSterse = .
  134. then
  135.   if $test .$MSlong = .1
  136.   then
  137.      $echo ""
  138.      $echo "  Total Average  Number Folder"
  139.      $echo "  ----- -------  ------ ------"
  140.      # We use MStrs here to place the spaces in columns that won't be
  141.      # converted to tabs by detab when this is checked into CVS
  142.      MStrs='"  ----- -------  ------\n%7d %7d %7d\n",\
  143.     gtotal,gtotal/gmessages,gmessages'
  144.   else
  145.      $echo ""
  146.      $echo "  Total  Number Folder"
  147.      $echo "  -----  ------ ------"
  148.      MStrs='"  -----  ------\n%7d %7d\n",gtotal,gmessages'
  149.   fi
  150. else
  151.   MStrs='""'
  152. fi
  153.  
  154. if $test .$MSlong = .1
  155. then MSlong='"%7d %7d %7d %s\n",total,total/messages,messages,folder'
  156. else MSlong='"%7d %7d %s\n",total,messages,folder'
  157. fi
  158.  
  159. TMPF=/tmp/maillog.$$
  160.  
  161. trap "rm -f $TMPF; exit 2" 1 2 3 13 15
  162. trap "rm -f $TMPF; exit 0" 0
  163.  
  164. ########
  165. #    And now we descend into the wonderful mix of shell-quoting and
  166. #    portable awk-programming :-)
  167. ########
  168.  
  169. rm -f $TMPF
  170. $cat >$TMPF <<HERE
  171. BEGIN {
  172.     FS="\\t";
  173.   }
  174.   { if(folder!=\$1)
  175.      { if(folder!="")
  176.       printf($MSlong);
  177.        gmessages+=messages;gtotal+=total;
  178.        messages=0;total=0;folder=\$1;
  179.      }
  180.     ++messages;total+=\$2;
  181.   }
  182. END {
  183.     if(folder!="")
  184.        printf($MSlong);
  185.     gmessages+=messages;gtotal+=total;
  186.     printf($MStrs);
  187.   }
  188. HERE
  189.  
  190. ########
  191. #    Only to end in a grand finale with your average sed script
  192. ########
  193.  
  194. if $test .$MSmergerror = .
  195. then
  196.   $sed    -e '/^From /d' -e '/^ [Ss][uU][bB][jJ][eE][cC][tT]:/d' \
  197.    -e '/^  Folder/s/        */    /' \
  198.    -e '/^  Folder/s/\/msg.[-0-9A-Za-z_][-0-9A-Za-z_]*    /\/    /' \
  199.    -e '/^  Folder/s/\/[0-9][0-9]*    /\/.    /' \
  200.    -e 's/^  Folder: \(.*\)/\1/' -e t -e 's/    /\\t/g' \
  201.    -e 's/^/ ## /' $OLDLOGFILE | $sort | $awk -f $TMPF -
  202. else
  203.   $sed    -e '/^From /d' -e '/^ [Ss][uU][bB][jJ][eE][cC][tT]:/d' \
  204.    -e '/^  Folder/s/        */    /' \
  205.    -e '/^  Folder/s/\/msg.[-0-9A-Za-z_][-0-9A-Za-z_]*    /\/    /' \
  206.    -e '/^  Folder/s/\/[0-9][0-9]*    /\/.    /' \
  207.    -e 's/^  Folder: \(.*\)/\1/' -e t \
  208.    -e 's/.*/ ## diagnostic messages ##/' $OLDLOGFILE | $sort | $awk -f $TMPF -
  209. fi
  210.  
  211. ########
  212. #    Nifty little script, isn't it?
  213. #    Now why didn't *you* come up with this truly trivial script? :-)
  214. ########
  215.